package grpc

import (
	"context"
	"geak/biz/lottery"
	"geak/biz/m"
	"geak/libs/ecode"
	"google.golang.org/grpc/status"
)



type Lottery struct {
	lottery.UnimplementedLotteryServer
}

func (c *Lottery) GetLastestLottery(ctx context.Context, in *lottery.GetLastestLotteryRequest) (*lottery.GetLastestLotteryResponse, error) {
	isDLT := in.GetDlt()
	isSSQ := in.GetSsq()
	lotteryList := make([]*m.Lottery,0,2)
	if isSSQ {
		ssq,err := lotterySrv.GetLastestSSQ()
		if err != nil {
			return nil,status.Errorf(ecode.SSQQueryError,"ssq query failure")
		}else {
			panic(ecode.LotteryFakeError)
			//return nil,status.Errorf(ecode.SSQQueryError,"ssq query failure")
			lotteryList = append(lotteryList, ssq)
		}
	}
	if isDLT {
		dlt, err := lotterySrv.GetLastestDLT()
		if err != nil {
			return nil,status.Errorf(ecode.DLTQueryError,"dlt query failure")
		} else {
			lotteryList = append(lotteryList, dlt)
		}
	}
	return &lottery.GetLastestLotteryResponse{Lottery:lotteryList},nil
}

func (this *Lottery) GetLotteries(ctx context.Context,
	in *lottery.GetLotteriesRequest) (*lottery.GetLotteriesResponse,error) {
	lotteryType := in.GetType()
	start := in.GetStart()
	count := in.GetCount()
	if count <= 0 || start <= 0 {
		return nil,status.Errorf(ecode.InvalidParam,"start and count should be larger than 0 ")
	}
	if lotteryType == 0 {
		// 双色球
		ssqlist,err := lotterySrv.GetSSQList(start, count)
		if err != nil {
			return nil,status.Errorf(ecode.SSQQueryError,"ssqlist query failure")
		}
		return &lottery.GetLotteriesResponse{Lottery:ssqlist},nil
	}else if lotteryType == 1 {
		dltlist,err := lotterySrv.GetDLTList(start,count)
		if err != nil {
			return nil,status.Errorf(ecode.DLTQueryError,"dltlist query failure")
		}
		return &lottery.GetLotteriesResponse{Lottery:dltlist},nil
	}
	return nil,status.Errorf(ecode.InvaildLotteryType,"type is invalid")
}

